/*
 * PatientMaster.java
 *
 * Created on __DATE__, __TIME__
 */

package ui;

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import java.sql.ResultSet;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;

import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.swing.JOptionPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import bean.ConnectionBean;
import bean.PrintingAttributeSet;

/**
 * 
 * @author __USER__
 */
public class PatientMaster extends javax.swing.JInternalFrame
{
	
	/** Creates new form PatientMaster */
	public PatientMaster()
	{
		initComponents();
	}
	
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{
		
		idSearchPanel = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		idPhoneNumber = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		idPatientName = new javax.swing.JTextField();
		idSearchButton = new javax.swing.JButton();
		jScrollPane2 = new javax.swing.JScrollPane();
		idDignosis = new javax.swing.JTextArea();
		jLabel3 = new javax.swing.JLabel();
		jScrollPane3 = new javax.swing.JScrollPane();
		idPrescription = new javax.swing.JTextArea();
		idPrintPrescription = new javax.swing.JButton();
		idPrintDignosis = new javax.swing.JButton();
		idUpdateInformation = new javax.swing.JButton();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		idDisease = new javax.swing.JTextField();
		idPrintAll = new javax.swing.JButton();
		jScrollPane1 = new javax.swing.JScrollPane();
		idHistory = new javax.swing.JTextPane();
		
		setClosable(true);
		setIconifiable(true);
		setMaximizable(true);
		setResizable(true);
		setTitle("Patient Master");
		setAutoscrolls(true);
		setDoubleBuffered(true);
		setMaximumSize(new java.awt.Dimension(800, 600));
		setMinimumSize(new java.awt.Dimension(800, 600));
		setName("PatientMaster");
		setPreferredSize(new java.awt.Dimension(800, 600));
		setVisible(true);
		
		idSearchPanel.setMaximumSize(new java.awt.Dimension(800, 600));
		idSearchPanel.setMinimumSize(new java.awt.Dimension(800, 600));
		
		jLabel1.setText("Phone Number");
		
		jLabel2.setText("Patient Name");
		
		idPatientName.setEditable(false);
		
		idSearchButton.setText("Search");
		idSearchButton.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idSearchButtonActionPerformed(evt);
			}
		});
		
		idDignosis.setColumns(20);
		idDignosis.setLineWrap(true);
		idDignosis.setRows(5);
		idDignosis.setWrapStyleWord(true);
		jScrollPane2.setViewportView(idDignosis);
		
		jLabel3.setText("Rx");
		
		idPrescription.setColumns(20);
		idPrescription.setLineWrap(true);
		idPrescription.setRows(5);
		idPrescription.setWrapStyleWord(true);
		jScrollPane3.setViewportView(idPrescription);
		
		idPrintPrescription.setText("Print Prescription");
		idPrintPrescription.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idPrintPrescriptionActionPerformed(evt);
			}
		});
		
		idPrintDignosis.setText("Print Diagnosis");
		idPrintDignosis.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idPrintDignosisActionPerformed(evt);
			}
		});
		
		idUpdateInformation.setText("Update");
		idUpdateInformation.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idUpdateInformationActionPerformed(evt);
			}
		});
		
		jLabel4.setText("Diagnosis");
		
		jLabel5.setText("Disease");
		
		idDisease.setPreferredSize(new java.awt.Dimension(76, 27));
		
		idPrintAll.setText("Print History");
		idPrintAll.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(java.awt.event.ActionEvent evt)
			{
				idPrintAllActionPerformed(evt);
			}
		});
		
		idHistory.setEditable(false);
		idHistory.setDoubleBuffered(true);
		jScrollPane1.setViewportView(idHistory);
		
		javax.swing.GroupLayout idSearchPanelLayout = new javax.swing.GroupLayout(idSearchPanel);
		idSearchPanel.setLayout(idSearchPanelLayout);
		idSearchPanelLayout.setHorizontalGroup(idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				idSearchPanelLayout.createSequentialGroup().addContainerGap().addGroup(
						idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
								idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
										idSearchPanelLayout.createSequentialGroup().addGroup(
												idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(
														idPhoneNumber, javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1,
														javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 111,
														Short.MAX_VALUE)).addGap(18, 18, 18).addGroup(
												idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
														jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE).addGroup(
														idSearchPanelLayout.createSequentialGroup().addComponent(idPatientName,
																javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
																javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE).addComponent(
																idSearchButton, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE))).addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)).addGroup(
										idSearchPanelLayout.createSequentialGroup().addComponent(jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED))).addGroup(
								idSearchPanelLayout.createSequentialGroup().addComponent(idPrintAll, javax.swing.GroupLayout.PREFERRED_SIZE, 161,
										javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))).addGroup(
						idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
								idSearchPanelLayout.createSequentialGroup().addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 71,
										javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(
										jLabel5).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(idDisease,
										javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
								idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addGroup(
										javax.swing.GroupLayout.Alignment.LEADING,
										idSearchPanelLayout.createSequentialGroup().addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25,
												javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE).addComponent(idPrintDignosis, javax.swing.GroupLayout.PREFERRED_SIZE, 156,
												javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(jScrollPane2,
										javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 357,
										javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
								idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addGroup(
										idSearchPanelLayout.createSequentialGroup().addComponent(idUpdateInformation).addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE).addComponent(idPrintPrescription, javax.swing.GroupLayout.PREFERRED_SIZE, 161,
												javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(jScrollPane3,
										javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 357,
										javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap(24, Short.MAX_VALUE)));
		idSearchPanelLayout.setVerticalGroup(idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				idSearchPanelLayout.createSequentialGroup().addGroup(
						idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(
								jLabel2)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(idPhoneNumber,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
								idPatientName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(idSearchButton).addComponent(jLabel5).addComponent(idDisease,
								javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
								jLabel4)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addGroup(
								idSearchPanelLayout.createSequentialGroup().addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 154,
										javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
										idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel3).addComponent(
												idPrintDignosis)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
										jScrollPane3)).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 372,
								javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
						idSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(idPrintAll).addComponent(
								idUpdateInformation).addComponent(idPrintPrescription)).addContainerGap(9, Short.MAX_VALUE)));
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(idSearchPanel,
				javax.swing.GroupLayout.PREFERRED_SIZE, 778, javax.swing.GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addComponent(idSearchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 490,
						javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(76, Short.MAX_VALUE)));
		
		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	private void idPrintDignosisActionPerformed(java.awt.event.ActionEvent evt)
	{
		try
		{
			String header = getHeaderText();
			String oldPrescription = idDignosis.getText();
			idDignosis.setText(header + "\n\n" + oldPrescription);
			idDignosis.print(null, null, true, null, PrintingAttributeSet.getPrintingAttributeSet(), true);
			idDignosis.setText(oldPrescription);
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(this, e);
		}
	}
	
	private void idPrintAllActionPerformed(java.awt.event.ActionEvent evt)
	{
		try
		{
			idHistory.print(null, null, true, null, PrintingAttributeSet.getPrintingAttributeSet(), true);
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(this, e);
		}
	}
	
	private void idPrintPrescriptionActionPerformed(java.awt.event.ActionEvent evt)
	{
		try
		{
			String header = getHeaderText();
			String oldPrescription = idPrescription.getText();
			idPrescription.setText(header + "\n\n" + oldPrescription);
			idPrescription.print(null, null, true, null, PrintingAttributeSet.getPrintingAttributeSet(), true);
			idPrescription.setText(oldPrescription);
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(this, e);
		}
	}
	
	private void idSearchButtonActionPerformed(java.awt.event.ActionEvent evt)
	{
		try
		{
			idDisease.setText("");
			idDignosis.setText("");
			idPrescription.setText("");
			Connection con = ConnectionBean.getConnection();
			ResultSet rsOne = con.createStatement().executeQuery("select name from patient where phone = " + idPhoneNumber.getText().trim());
			idPatientName.setText("");
			if (rsOne.next())
			{
				idPatientName.setText(rsOne.getString("name").trim());
			}
			else
			{
				JOptionPane.showMessageDialog(this, "patient Not found, Please add this patient first");
			}
			if (idPhoneNumber.getText().trim().length() > 0)
			{
				String phone_no = "";
				String disease = "";
				idHistory.setText("");
				StyledDocument styleDoc = idHistory.getStyledDocument();
				SimpleAttributeSet styleSet = new SimpleAttributeSet();
				styleDoc.insertString(styleDoc.getLength(), getHeaderText() + "\n\n", styleSet);
				PreparedStatement psm = con.prepareStatement("select * from dignosis where phone_no = ?");
				psm.setString(1, idPhoneNumber.getText().trim());
				ResultSet rs = psm.executeQuery();
				while (rs.next())
				{
					phone_no = rs.getString("phone_no");
					StyleConstants.setBold(styleSet, true);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					styleDoc.insertString(styleDoc.getLength(), "Dignosis Date : ", styleSet);
					StyleConstants.setBold(styleSet, false);
					styleDoc.insertString(styleDoc.getLength(),
							new SimpleDateFormat("dd-MMM-yyyy").format(new SimpleDateFormat("yyyy-MM-dd").parse(rs.getString("dignosis_date"))),
							styleSet);
					StyleConstants.setBold(styleSet, true);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					disease = rs.getString("disease");
					styleDoc.insertString(styleDoc.getLength(), "Disease: ", styleSet);
					StyleConstants.setBold(styleSet, false);
					styleDoc.insertString(styleDoc.getLength(), disease, styleSet);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					StyleConstants.setBold(styleSet, true);
					styleDoc.insertString(styleDoc.getLength(), "Dignosis : ", styleSet);
					StyleConstants.setBold(styleSet, false);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					styleDoc.insertString(styleDoc.getLength(), rs.getString("dignosis"), styleSet);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					StyleConstants.setBold(styleSet, true);
					styleDoc.insertString(styleDoc.getLength(), "Prescription : ", styleSet);
					StyleConstants.setBold(styleSet, false);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					styleDoc.insertString(styleDoc.getLength(), rs.getString("prescription"), styleSet);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
					styleDoc.insertString(styleDoc.getLength(), "======================", styleSet);
					styleDoc.insertString(styleDoc.getLength(), "\n", styleSet);
				}
				if (phone_no.trim().length() > 0) idPhoneNumber.setText(phone_no);
				idDisease.setText(disease);
			}
			else
			{
				JOptionPane.showMessageDialog(this, "Please provide Patient Phone Number or Patient Name", "Inputs required",
						JOptionPane.ERROR_MESSAGE);
			}
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(this, e);
		}
	}
	
	private void idUpdateInformationActionPerformed(java.awt.event.ActionEvent evt)
	{
		if (idPrescription.getText().trim().length() == 0 || idDignosis.getText().trim().length() == 0
				|| idPhoneNumber.getText().trim().length() == 0 || idPatientName.getText().trim().length() == 0
				|| idDisease.getText().trim().length() == 0)
		{
			JOptionPane.showMessageDialog(this, "Please provide All the information");
			return;
		}
		int confirm = JOptionPane.showConfirmDialog(this, "Do you want to update the disnosis");
		if (confirm == JOptionPane.YES_OPTION)
		{
			Connection con = ConnectionBean.getConnection();
			PreparedStatement psm;
			try
			{
				psm =
						con.prepareStatement("insert into dignosis (phone_no, patient_name, dignosis_date, dignosis, prescription, disease) values (?,?,?,?,?,?)");
				psm.setString(1, idPhoneNumber.getText().trim());
				psm.setString(2, idPatientName.getText().trim());
				psm.setDate(3, new java.sql.Date(new java.util.Date().getTime()));
				psm.setString(4, idDignosis.getText());
				psm.setString(5, idPrescription.getText());
				psm.setString(6, idDisease.getText().trim());
				int i = psm.executeUpdate();
				if (i == 1)
				{
					JOptionPane.showMessageDialog(this, "Information Updated Successfully");
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Information Not Updated please check history and contact the developer");
				}
			}
			catch (SQLException e)
			{
				JOptionPane.showMessageDialog(this, e);
			}
		}
	}
	
	private String getHeaderText()
	{
		String header = "";
		try
		{
			Connection con = ConnectionBean.getConnection();
			PreparedStatement psm = con.prepareStatement("Select * from patient where phone = ?");
			psm.setString(1, idPhoneNumber.getText().trim());
			ResultSet rs = psm.executeQuery();
			if (rs.next())
			{
				Long diviser = 1000 * 60 * 60 * 24 * 365l;
				String age = "" + ((float)new java.util.Date().getTime() - rs.getDate("DOB").getTime()) / (float)diviser;
				String years = "0";
				int months = 0;
				if (age.contains("."))
				{
					years = "" + Integer.parseInt(age.split("\\.")[0]);
					months = (int)(Double.parseDouble("." + age.split("\\.")[1]) * 12);
				}
				else
				{
					years = age;
				}
				
				header =
						"Name: " + idPatientName.getText().trim() + "   Age: " + years + " Years and " + months + " Months  Sex:"
								+ rs.getString("sex");
			}
		}
		catch (Exception e)
		{
			JOptionPane.showMessageDialog(this, e);
		}
		return header;
	}
	
	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTextArea idDignosis;
	private javax.swing.JTextField idDisease;
	private javax.swing.JTextPane idHistory;
	private javax.swing.JTextField idPatientName;
	private javax.swing.JTextField idPhoneNumber;
	private javax.swing.JTextArea idPrescription;
	private javax.swing.JButton idPrintAll;
	private javax.swing.JButton idPrintDignosis;
	private javax.swing.JButton idPrintPrescription;
	private javax.swing.JButton idSearchButton;
	private javax.swing.JPanel idSearchPanel;
	private javax.swing.JButton idUpdateInformation;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	// End of variables declaration//GEN-END:variables
	
}
